package org.tpc.yn.db;

import com.google.common.base.CaseFormat;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class DBManager {
    public static List<Map<String, Object>> resultSet2List(ResultSet rs) {
        return resultSet2List(rs , false);
    }

    public static List<Map<String, Object>> resultSet2List(ResultSet rs , boolean lowerCase) {
        List<Map<String, Object>> dataList = null;
        if (rs == null) {
            return dataList;
        }
        try {

            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();

            if (rs.next()) {
                dataList = new ArrayList<Map<String, Object>>();
                do {
                    Map<String, Object> rowMap = new LinkedHashMap<String, Object>();
                    for (int i = 1; i <= columnCount; i++) {
                        String columnLabel = rsmd.getColumnLabel(i);// sql查询的别名
                        if(lowerCase){
                            columnLabel = columnLabel.toLowerCase();
                        }
                        Object value = rs.getObject(columnLabel);

                        //判断如果是空值，则显示为“”，避免在页面上显示null的
                        if(value == null){
                            columnLabel = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, columnLabel);
                            rowMap.put(columnLabel, "");
                            continue;
                        }
                        if(value instanceof String){
                            String str = (String) value;
                            if(StringUtils.isEmpty(str)){
                                rowMap.put(columnLabel, "");
                                continue;
                            }
                        }
                        columnLabel = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, columnLabel);
                        rowMap.put(columnLabel, value);
                    }
                    dataList.add(rowMap);
                } while (rs.next());

            }

        } catch (Exception e) {
            e.printStackTrace();
            //TODO 记录日志
        }
        return dataList;
    }

    public static List<Map<String, Object>> resultSet2List(ResultSet rs , boolean lowerCase,Boolean to) {
        List<Map<String, Object>> dataList = null;
        if (rs == null) {
            return dataList;
        }
        try {

            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();

            if (rs.next()) {
                dataList = new ArrayList<Map<String, Object>>();
                do {
                    Map<String, Object> rowMap = new LinkedHashMap<String, Object>();
                    for (int i = 1; i <= columnCount; i++) {
                        String columnLabel = rsmd.getColumnLabel(i);// sql查询的别名
                        if(lowerCase){
                            columnLabel = columnLabel.toLowerCase();
                        }
                        Object value = rs.getObject(columnLabel);

                        //判断如果是空值，则显示为“”，避免在页面上显示null的
                        if(value == null){
                            if(to){
//                                columnLabel = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, columnLabel);
                                rowMap.put(columnLabel, "");
                                continue;
                            }else{
                                columnLabel = columnLabel;
                                rowMap.put(columnLabel, "");
                                continue;
                            }

                        }
                        if(value instanceof String){
                            String str = (String) value;
                            if(StringUtils.isEmpty(str)){
                                rowMap.put(columnLabel, "");
                                continue;
                            }
                        }
                        if(to){
//                            columnLabel = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, columnLabel);
                            columnLabel = columnLabel;
                        }else{
                            columnLabel = columnLabel;
                        }

                        rowMap.put(columnLabel, value);
                    }
                    dataList.add(rowMap);
                } while (rs.next());

            }

        } catch (Exception e) {
            e.printStackTrace();
            //TODO 记录日志
        }
        return dataList;
    }
}
